package comWorld.user.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import comWorld.user.manage.UserInfoManage;

/**
 * Servlet implementation class Logout
 */
@WebServlet("/user/logout")
public class Logout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logout() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserInfoManage.updateUserStatus(session, null);
		Cookie[] cks = request.getCookies();
		for (Cookie ck : cks) {
			if (ck.getName().equals("email") || ck.getName().equals("password")) {
				ck.setMaxAge(0);
				response.addCookie(ck);
			}
		}
		String  gotoPage = request.getParameter("goto_page");
		response.sendRedirect(gotoPage);
	}

}
